<template>
	<view class="content page-bg">
		<view class="updatePhone">

			<view class="body">

				<input type="tel" maxlength="11" placeholder=" 请填写手机号" v-model="formData.mobile"
					class="user-name-input" />

				<view class="user-code">
					<input type="number" maxlength="4" placeholder="请填写验证码" v-model="formData.code"
						class="user-name-input" />
					<button class="sms-btn" @tap="showVerify" :disabled="codeDisabled">{{codeTips}}</button>
					<Verify @success="getCode" :mode="'pop'" :captchaType="'blockPuzzle'"
						:imgSize="{ width: '330px', height: '155px' }" ref="verify"></Verify>
					<u-code ref="uCode" @change="codeChange" seconds="60" @start="codeDisabled = true"
						@end="codeDisabled = false"></u-code>
				</view>
				<button class="contact-btn" @click="bindPhone">登录</button>
			</view>
		</view>
		<div class="app-info">
			<image src="../../static/logo.svg" class="app-logo" mode=""></image>
			<text class="app-tips">本程序仅供娱乐，请勿用于非法活动</text>
			<text class="app-tips">远离赌博，快乐生活</text>
		</div>
	</view>
</template>

<script>
	import watermark from 'watermark-dom'
	import Verify from "../../components/verifition/Verify"
	import {
		bindMobile
	} from "../../api/user.js"
	import {
		webSmsLogin,
		sendSmsCode
	} from "../../api/auth.js"
	export default {
		components: {
			Verify
		},
		data() {
			return {
				codeDisabled: false,
				codeTips: '',
				formData: {
					mobile: '',
					password: '',
					code: ''
				},
			}
		},
		onLoad() {
			watermark.init({
				watermark_txt: "记分工具人",
				"watermark_rows": 10, //水印行数
				"watermark_cols": 10, //水印列数
				"watermark_angle": 30, //水印倾斜度数
			})
			let tokenExpiresTime = uni.getStorageSync('TOKEN_EXPIRESTIME')
			console.log("tokenExpiresTime", tokenExpiresTime)
			if (tokenExpiresTime) {
				console.log("new Date().getTime()", new Date().getTime())
				this.$store.dispatch('ObtainUserInfo').then(result => {
					this.jumpNext()
				})
			}

		},
		methods: {
			codeChange(text) {
				this.codeTips = text
			},

			showVerify() {
				const mobile = this.formData.mobile
				if (!mobile) {
					uni.$u.toast('请填写手机号')
				} else if (!uni.$u.test.mobile(mobile)) {
					uni.$u.toast('手机号格式不正确')
				} else if (this.$refs.uCode.canGetCode) {
					this.$refs.verify.show()
				} else {
					uni.$u.toast('倒计时结束后再发送')
				}
			},
			getCode(captchaParams) {

				// 模拟向后端请求验证码
				uni.showLoading({
					title: '正在获取验证码'
				})
				//scene:1登陆获取验证码场景
				sendSmsCode({
					mobile: this.formData.mobile,
					scene: 5,
					captchaVerification: captchaParams.captchaVerification

				}).then(res => {
					//console.log(res)
					uni.hideLoading()
					uni.$u.toast('验证码已发送')
					// 通知验证码组件内部开始倒计时
					this.$refs.uCode.start()
				})

			},
			bindPhone() {
				const mobile = this.formData.mobile
				if (!mobile) {
					uni.$u.toast('请填写手机号')
					return
				}
				if (!uni.$u.test.mobile(mobile)) {
					uni.$u.toast('手机号格式不正确')
					return
				}
				if (!this.formData.code) {
					uni.$u.toast('请填写验证码')
					return
				}

				this.$store.dispatch('Login', {
					type: 1,
					data: this.formData
				}).then(res => {

					this.$store.dispatch('ObtainUserInfo').then(result => {
						this.jumpNext()
					})


				})

			},
			jumpNext() {
				uni.redirectTo({
					url: "/pages/index/index"
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		// background-color: #f2f2f2;
		width: 100vw;
		height: 100vh;
		position: relative;

		.updatePhone::after {
			display: table;
			content: "";
			clear: both;
		}

		.updatePhone {
			background-color: white;
			border-radius: 30upx;

			width: 700upx;




			.body {
				border-radius: 30upx;
				padding-left: 24upx;
				padding-right: 24upx;
				margin-bottom: 20upx;
				padding-top: 24upx;
				background-color: #f2f7ff;

				.user-name-input {
					background-color: #fff;
					border-radius: 12upx;
					box-shadow: 0 5upx 10upx 0 rgba(0, 0, 0, .05);
					font-size: 30upx;
					line-height: 30upx;
					height: 80upx;
					flex-grow: 1;
					padding-left: 12upx;
					padding-right: 12upx;

				}

				.user-code {
					display: flex;
					align-items: center;
				}

				.sms-btn {
					width: 250upx;
					font-size: 30upx;
					height: 80upx;


					margin: 40upx auto;
					margin-left: 24upx;
					background-color: $uni-color-primary;
					color: white;
					border-radius: 8upx;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}



			.contact-btn {
				font-size: 30upx;
				height: 80upx;
				line-height: 30upx;
				margin: 40upx auto;
				background-color: $uni-color-primary;
				color: white;
				border-radius: 8upx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}

		.app-info {
			bottom: 64upx;
			position: absolute;
			text-align: center;
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;

			.app-logo {
				height: 60upx;
				margin-bottom: 20upx;
			}

			.app-tips {
				color: #00000050;
				font-size: 24upx;

			}
		}
	}
</style>